package cn.com.bluemoon.daps.sync.controller.api;

import cn.com.bluemoon.daps.api.sync.RemoteSyncService;
import cn.com.bluemoon.daps.common.annotation.FcApiFor;
import cn.com.bluemoon.daps.common.constant.ServiceNameConstants;
import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.domain.UserInfoHolder;
import cn.com.bluemoon.daps.sync.base.ApplyPassGroup;
import cn.com.bluemoon.daps.sync.base.ApplyRevertGroup;
import cn.com.bluemoon.daps.sync.base.ApplyUnPassGroup;
import cn.com.bluemoon.daps.sync.service.DapModelApplyPublishService;
import cn.com.bluemoon.daps.sync.vo.applypublish.ApplyPublishParam;
import cn.com.bluemoon.daps.sync.vo.applypublish.ApplyPublishResultVo;
import cn.com.bluemoon.daps.sync.vo.applypublish.ModelCompareVo;
import cn.com.bluemoon.daps.sync.vo.applypublish.TableApplyPublishReportVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static cn.com.bluemoon.daps.sync.entity.DapSyncProdEnvTask.COMPARE_VERSION_KEY;


/**
 * @author Jarod.Kong
 * @date 2021/6/16 16:57
 */
@Api(tags = "FeignClient服务-同步服务")
@RestController
@Slf4j
@Validated
@RequestMapping(ServiceNameConstants.DapsApiSync.API_SYNC)
public class DapModelApplyPublishHelperApi implements RemoteSyncService {
    @Resource
    private DapModelApplyPublishService applyPublishService;

    @BmPermission(value = {""}, name = "申请发布-模型版本对比前-决策-最新模型表是否与语法问题", ignore = true)
    @PostMapping("/application/compareV/predicate")
    @ApiOperation(value = "申请发布-模型版本对比前-决策-最新模型表是否与语法问题-不为空则有语法问题")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class),
            @ApiResponse(code = 200, message = "ok", responseContainer = "List", response = TableApplyPublishReportVo.class)})
    @FcApiFor
    public ResultBean<List<TableApplyPublishReportVo>> modelTableLayerPredicate(@RequestParam(value = "modelId") @ApiParam(value = "模型id") String modelId) {
        ApplyPublishResultVo res = applyPublishService.modelTableLayerPredicate(modelId, true, false, UserInfoHolder.getUserInfoOrEmpty());
        return ResultBean.ok(res.getReportVos());
    }

    @BmPermission(value = {""}, name = "申请发布-模型版本对比", ignore = true)
    @PostMapping("/application/compareV")
    @ApiOperation(value = "申请发布-模型版本对比（最新版（new）vs最新已发布版（old）")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class), @ApiResponse(code = 200, message = "ok", responseContainer = "List", response = ModelCompareVo.class)})
    @FcApiFor
    public ResultBean<List<ModelCompareVo>> compareModelVersion(@RequestParam(value = "modelId") @ApiParam(value = "模型id") String modelId,
                                                                @RequestParam(value = "newVsOld", required = false, defaultValue = "true")
                                                                @ApiParam(value = "true 为最新版（new）对比 最新已发布版（old），false 为最新已发布版（old）对比 最新版（new）， 默认为true") Boolean newVsOld,
                                                                @RequestParam(value = "forceOverride", required = false, defaultValue = "false")
                                                                @ApiParam(value = "是否强制拉取对比且覆盖上一次对比结果，true是 false否，默认为false") Boolean forceOverride) {
        if (newVsOld == null) newVsOld = true;
        if (forceOverride == null) forceOverride = false;
        ApplyPublishResultVo res = applyPublishService.compareModelVersion(modelId, newVsOld, forceOverride, UserInfoHolder.getUserInfoOrEmpty());
        return Boolean.TRUE.equals(res.getApplyPublish()) ?
                ResultBean.ok(res.getModelCompareVos()).addMoreData(COMPARE_VERSION_KEY, res.getCompareVersion()) :
                ResultBean.error(res.getMessage());
    }

    @BmPermission(value = {""}, name = "申请发布-模型版本对比V2", ignore = true)
    @PostMapping("/application/compareV2")
    @ApiOperation(value = "申请发布-模型版本对比V2-语法检查报告（最新版（new）vs最新已发布版（old）")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class), @ApiResponse(code = 200, message = "ok", response = ApplyPublishResultVo.class)})
    @FcApiFor
    @Deprecated
    public ResultBean<ApplyPublishResultVo> compareModelVersionV2(@RequestParam(value = "modelId") @ApiParam(value = "模型id") String modelId,
                                                                  @RequestParam(value = "newVsOld", required = false, defaultValue = "true")
                                                                  @ApiParam(value = "true 为最新版（new）对比 最新已发布版（old），false 为最新已发布版（old）对比 最新版（new）， 默认为true") Boolean newVsOld,
                                                                  @RequestParam(value = "forceOverride", required = false, defaultValue = "false")
                                                                  @ApiParam(value = "是否强制拉取对比且覆盖上一次对比结果，true是 false否，默认为false") Boolean forceOverride) {
        if (newVsOld == null) newVsOld = true;
        if (forceOverride == null) forceOverride = false;
        ApplyPublishResultVo res = applyPublishService.compareModelVersion(modelId, newVsOld, forceOverride, UserInfoHolder.getUserInfoOrEmpty());
        return ResultBean.ok(res);
    }

    @BmPermission(value = {""}, name = "申请发布-确认通过", ignore = true)
    @PostMapping("/application/pass")
    @ApiOperation(value = "申请发布-确认通过")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class), @ApiResponse(code = 200, message = "ok", response = String.class)})
    @FcApiFor
    public ResultBean<String> applyConfirmPass(@Validated(value = {ApplyPassGroup.class}) @RequestBody ApplyPublishParam applyPublishParam) {
        applyPublishService.applyConfirmPass(applyPublishParam, UserInfoHolder.getUserInfoOrEmpty());
        return ResultBean.ok();
    }

    @BmPermission(value = {""}, name = "申请发布-确认不通过", ignore = true)
    @ApiOperation(value = "申请发布-确认不通过")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class), @ApiResponse(code = 200, message = "ok", response = String.class)})
    @Override
    public ResultBean<String> applyConfirmUnPass(@Validated(value = {ApplyUnPassGroup.class}) @RequestBody ApplyPublishParam applyPublishParam) {
        applyPublishService.applyConfirmUnPass(applyPublishParam, UserInfoHolder.getUserInfoOrEmpty());
        return ResultBean.ok();
    }

    @BmPermission(value = {""}, name = "申请发布-撤销", ignore = true)
    @ApiOperation(value = "申请发布-撤销")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class), @ApiResponse(code = 200, message = "ok", response = String.class)})
    @FcApiFor
    public ResultBean<String> applyRevert(@Validated(value = {ApplyRevertGroup.class}) @RequestBody ApplyPublishParam applyPublishParam) {
        applyPublishService.applyUnPass(applyPublishParam, UserInfoHolder.getUserInfoOrEmpty());
        return ResultBean.ok();
    }
}
